package com.dragon.modules.yuegou.client.wechat_mini.security.impl;

import com.dragon.modules.yuegou.client.wechat_mini.model.AccessToken;
import com.dragon.modules.yuegou.client.wechat_mini.security.AccessTokenRepository;
import com.dragon.modules.yuegou.web.Check;
import org.springframework.cache.Cache;

/**
 * 缓存AccessToken仓库
 * 
 * @author liuwu
 * @date 2019年7月6日
 *
 */
public class CacheAccessTokenRepositoryImpl implements AccessTokenRepository {

	private final Cache cache;

	/**
	 * 构造仓库
	 * 
	 * @param cache 缓存
	 */
	public CacheAccessTokenRepositoryImpl(Cache cache) {
		super();
		Check.notNull(cache, "cache is required");
		this.cache = cache;
	}

	@Override
	public AccessToken get(String appid) {
		return cache.get(appid, AccessToken.class);
	}

	@Override
	public AccessToken save(String appid, AccessToken accessToken) {
		cache.put(appid, accessToken);
		return accessToken;
	}
}
